<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JS文档的解析:async、defer 属性 | Jaho’s Blog</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="记录日常学习工作">
    
    <link rel="preload" href="/assets/css/0.styles.5b8fe0a4.css" as="style"><link rel="preload" href="/assets/js/app.6514719e.js" as="script"><link rel="preload" href="/assets/js/2.ea114ffb.js" as="script"><link rel="preload" href="/assets/js/21.5c50aa1c.js" as="script"><link rel="preload" href="/assets/js/4.e093c564.js" as="script"><link rel="prefetch" href="/assets/js/10.13203aa5.js"><link rel="prefetch" href="/assets/js/11.099868c4.js"><link rel="prefetch" href="/assets/js/12.c8365e17.js"><link rel="prefetch" href="/assets/js/13.89751e7c.js"><link rel="prefetch" href="/assets/js/14.31f563eb.js"><link rel="prefetch" href="/assets/js/15.08b381b6.js"><link rel="prefetch" href="/assets/js/16.1a3867de.js"><link rel="prefetch" href="/assets/js/17.7dd4e0c6.js"><link rel="prefetch" href="/assets/js/18.907c4597.js"><link rel="prefetch" href="/assets/js/19.fac8aa1a.js"><link rel="prefetch" href="/assets/js/20.73fe0e75.js"><link rel="prefetch" href="/assets/js/22.83d03663.js"><link rel="prefetch" href="/assets/js/23.061e7ac0.js"><link rel="prefetch" href="/assets/js/24.cc7e9011.js"><link rel="prefetch" href="/assets/js/25.fa6c7aef.js"><link rel="prefetch" href="/assets/js/26.39392f70.js"><link rel="prefetch" href="/assets/js/27.204436be.js"><link rel="prefetch" href="/assets/js/28.3f54c157.js"><link rel="prefetch" href="/assets/js/29.799d048c.js"><link rel="prefetch" href="/assets/js/3.6c5d6214.js"><link rel="prefetch" href="/assets/js/30.deed02f3.js"><link rel="prefetch" href="/assets/js/31.5916873b.js"><link rel="prefetch" href="/assets/js/32.b0634144.js"><link rel="prefetch" href="/assets/js/33.734675b8.js"><link rel="prefetch" href="/assets/js/34.20404dc4.js"><link rel="prefetch" href="/assets/js/35.d93b829f.js"><link rel="prefetch" href="/assets/js/36.b0692d8d.js"><link rel="prefetch" href="/assets/js/37.3dffa306.js"><link rel="prefetch" href="/assets/js/38.04649a97.js"><link rel="prefetch" href="/assets/js/39.7dfcdc32.js"><link rel="prefetch" href="/assets/js/40.04914511.js"><link rel="prefetch" href="/assets/js/41.ced85449.js"><link rel="prefetch" href="/assets/js/42.72739213.js"><link rel="prefetch" href="/assets/js/43.de1cff16.js"><link rel="prefetch" href="/assets/js/44.48e301ae.js"><link rel="prefetch" href="/assets/js/45.870a7650.js"><link rel="prefetch" href="/assets/js/46.657f15b8.js"><link rel="prefetch" href="/assets/js/47.3df1bfdb.js"><link rel="prefetch" href="/assets/js/48.f4e4e774.js"><link rel="prefetch" href="/assets/js/49.632bf611.js"><link rel="prefetch" href="/assets/js/5.6531e181.js"><link rel="prefetch" href="/assets/js/50.a8018493.js"><link rel="prefetch" href="/assets/js/51.cb424f59.js"><link rel="prefetch" href="/assets/js/52.a9f595dc.js"><link rel="prefetch" href="/assets/js/53.cf8a9ec0.js"><link rel="prefetch" href="/assets/js/54.46d774a3.js"><link rel="prefetch" href="/assets/js/55.3574f9b8.js"><link rel="prefetch" href="/assets/js/56.baf60ba5.js"><link rel="prefetch" href="/assets/js/57.af42b559.js"><link rel="prefetch" href="/assets/js/58.9300cb59.js"><link rel="prefetch" href="/assets/js/6.00f437ec.js"><link rel="prefetch" href="/assets/js/7.58ac7166.js"><link rel="prefetch" href="/assets/js/8.1b9c5002.js"><link rel="prefetch" href="/assets/js/9.5c076362.js">
    <link rel="stylesheet" href="/assets/css/0.styles.5b8fe0a4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Jaho’s Blog" class="logo"> <span class="site-name can-hide">Jaho’s Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/posts/" aria-current="page" class="sidebar-link">写在前面</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>工作问题解决</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>JavaScript</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/posts/JavaScript/闭包作用域.html" class="sidebar-link">了解闭包及作用域</a></li><li><a href="/posts/JavaScript/短路原则.html" class="sidebar-link">布尔判定和短路原则</a></li><li><a href="/posts/JavaScript/类型转换.html" class="sidebar-link">类型转换</a></li><li><a href="/posts/JavaScript/数组转树.html" class="sidebar-link">数组转树方案</a></li><li><a href="/posts/JavaScript/数组拍平.html" class="sidebar-link">数组拍平方案</a></li><li><a href="/posts/JavaScript/数组求和.html" class="sidebar-link">数组求和（不允许使用循环）</a></li><li><a href="/posts/JavaScript/async.html" class="sidebar-link">async await实战</a></li><li><a href="/posts/JavaScript/defer.html" aria-current="page" class="active sidebar-link">JS文档的解析:async、defer 属性</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/posts/JavaScript/set.html" class="sidebar-link">Set笔记</a></li><li><a href="/posts/JavaScript/map.html" class="sidebar-link">Map笔记</a></li><li><a href="/posts/JavaScript/call.html" class="sidebar-link">手写call、bind、apply</a></li><li><a href="/posts/JavaScript/eventBus.html" class="sidebar-link">Eventbus使用</a></li><li><a href="/posts/JavaScript/instanceof.html" class="sidebar-link">浅析instanceof的实现</a></li><li><a href="/posts/JavaScript/function.html" class="sidebar-link">如何优雅的用function实现一个class</a></li><li><a href="/posts/JavaScript/require.html" class="sidebar-link">浅析commonjs中require的执行方式</a></li><li><a href="/posts/JavaScript/promise.html" class="sidebar-link">手写Promise</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Css</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nodejs</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="js文档的解析-async、defer-属性"><a href="#js文档的解析-async、defer-属性" class="header-anchor">#</a> JS文档的解析:async、defer 属性</h1> <h3 id="前言"><a href="#前言" class="header-anchor">#</a> 前言</h3> <p>让我们来复习一下<code>&lt;script&gt;</code>标签的基本使用</p> <h3 id="html中使用-script-元素插入-javascript脚本-通常具有这几个主要属性"><a href="#html中使用-script-元素插入-javascript脚本-通常具有这几个主要属性" class="header-anchor">#</a> HTML中使用<code>&lt;script&gt;</code>元素插入 JavaScript脚本，通常具有这几个主要属性</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token number">1.</span> src：表示包含要执行代码的外部文件
<span class="token number">2.</span> <span class="token keyword">async</span>：立即下载脚本，但不应妨碍页面中的其他操作，比如下载其他资源或等待加载其他脚本
<span class="token number">3.</span> defer：延迟到<span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">文档完全被解析和显示</span><span class="token template-punctuation string">`</span></span>之后再执行。
</code></pre></div><p>async和defer只对外部脚本文件有效。
如果要通过script元素来包含外部 JavaScript 文件，src 指向外部 JavaScript 文件的链接</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">&quot;text/javascript&quot;</span> src<span class="token operator">=</span><span class="token string">&quot;example.js&quot;</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
</code></pre></div><h3 id="script-标签小误区"><a href="#script-标签小误区" class="header-anchor">#</a> <code>&lt;script&gt;</code>标签小误区</h3> <p>不能在script中出现<code>&lt;/script&gt;</code>字符串</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">&quot;text/javascript&quot;</span><span class="token operator">&gt;</span>

<span class="token keyword">function</span> <span class="token function">sayScript</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;/script&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;\/script&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//应该使用转义符的方式</span>

<span class="token punctuation">}</span>

<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
</code></pre></div><p>标签使用时需要规范</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">&quot;text/javascript&quot;</span> src<span class="token operator">=</span><span class="token string">&quot;my.js&quot;</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">&quot;text/javascript&quot;</span> src<span class="token operator">=</span><span class="token string">&quot;my.js&quot;</span> <span class="token operator">/</span><span class="token operator">&gt;</span><span class="token comment">//这种格式不规范，浏览器不能得到正确的解析</span>
</code></pre></div><h3 id="script-标签的位置"><a href="#script-标签的位置" class="header-anchor">#</a> <code>&lt;script&gt;</code>标签的位置</h3> <p>按照传统的做法，所有<code>&lt;script&gt;</code>元素都应该放在页面的<code>&lt;head&gt;</code>元素中，的目的就是把所有外部文件（ CSS 、JavaScript）的引用都放在相同的地方但是这样会导致用户在打开页面的时候，由于浏览器需要时间加载JS代码，导致dom无法立即显示，因为浏览器是在遇到body标签时候才开始呈现内容的
为了避免这个问题，一般我们把 JavaScript 的引用放在<code>&lt;body&gt;</code>元素中页面内容的后面</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token constant">DOCTYPE</span> html<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>html<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>head<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>title<span class="token operator">&gt;</span>Example <span class="token constant">HTML</span> Page<span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>body<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">&quot;text/javascript&quot;</span> src<span class="token operator">=</span><span class="token string">&quot;example1.js&quot;</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">&quot;text/javascript&quot;</span> src<span class="token operator">=</span><span class="token string">&quot;example2.js&quot;</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">&gt;</span>
</code></pre></div><h3 id="defer、async属性"><a href="#defer、async属性" class="header-anchor">#</a> defer、async属性</h3> <ul><li>没有defer、async属性的情况下，HTML 文件将被解析，解析到script标签时如果没有延迟和异步属性，则停下html的解析，开始<code>解析</code>JS文件<code>并且执行</code>，等到JS文件执行完后继续解析html，执行的过程可以用图解简洁的看出来。
<img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2eac0e62c10e416ca3730105c2ee6ab5~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"> <code>defer、async属性只适用于外链JS的方式</code></li> <li><code>defer</code>属性在 HTML 解析期间下载文件，会将JS延迟到页面解析完后再执行。相当于即使再head中设置了标签，但是浏览器还是会等遇到html标签再去执行有defer属性的JS，<code>defer</code>脚本也保证按照它们在文档中出现的顺序执行，但是实际上设置了defer属性的延迟JS脚本不一定会按照顺序执行，所以红宝书中也指出<code>最好只包含一个延迟defer属性的JS</code>。</li></ul> <p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1fd6921334844ab1b45e7cf6f33c41df~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <ul><li><code>async</code>的解析和html的解析是并行的，解析完会立刻执行。</li></ul> <p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/08cef9d34f7142128639ac5dfba051df~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <h3 id="面试提问-js的解析是否会影响文档的解析"><a href="#面试提问-js的解析是否会影响文档的解析" class="header-anchor">#</a> 面试提问:<code>JS的解析</code>是否会影响<code>文档的解析？</code></h3> <p>注意题目说的是解析，所以我们可以只看蓝色部分，但作为面试，我们应该将解析和执行俩种情况都说出来</p> <ol><li>在<code>没有设置其他属性</code>的情况下，<code>JS如果放在头部</code>，那么<code>JS的解析和执行都会影响文档的解析</code>,</li> <li>如果设置了<code>defer</code>属性，那么<code>JS的解析和文档的解析是并行</code>的，并且会在文档解析完进行执行JS<br><code>JS的解析和执行不会影响文档的解析</code>。</li> <li>如果设置了<code>async</code>属性，那么<code>JS的解析和文档的解析也是并行</code>的，<code>但是JS的解析速度会决定是否影响文档的解析</code>。<br>如果<code>JS解析完了HTML文档还没解析完</code>，那么<code>JS执行会影响文档解析</code>。<br>如果<code>HTML文档解析完了JS还没解析完</code>，那么<code>JS的执行不会影响文档解析</code>。</li></ol> <p>如果是在尾部则不会影响，因为浏览器解析到JS的时候文档已经解析完了。</p> <p>参考<a href="https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html" target="_blank" rel="noopener noreferrer">链接<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-01-04 11:02:15</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/posts/JavaScript/async.html" class="prev">
        async await实战
      </a></span> <span class="next"><a href="/posts/JavaScript/set.html">
        Set笔记
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.6514719e.js" defer></script><script src="/assets/js/2.ea114ffb.js" defer></script><script src="/assets/js/21.5c50aa1c.js" defer></script><script src="/assets/js/4.e093c564.js" defer></script>
  </body>
</html>
